#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "mylog.h"

//通过全局变量保存配置文件的信息
static config_info my_config_info; 
config_info* get_config()
{
	return &my_config_info;
}

static parse_config* parse_config_list;
//配置信息初始化
int config_info_init()
{
	strncpy(my_config_info.output_file,CONFIG_FILE_PATH,MEDIUMSTR);
	my_config_info.default_level=INFO;
	int i;
	for(i=1;i<5;i++) my_config_info.format[i]=-1;
	my_config_info.format[0]=4;
	my_config_info.max_size=10000;
	my_config_info.save_log=0;
	parse_config_list=NULL;
}

//注册一个解析选项的函数
int regist_option(char *option_name,do_option_t do_option_func)
{
	if(option_name==NULL||!do_option_func)
	  return ARGS_ERROR;
	parse_config* node=malloc(sizeof(parse_config));
	if(node==NULL)
	  return MALLOC_ERROR;
	node->option_name=strdup(option_name);
	if(node->option_name==NULL)
	  {
          free(node);
		  return MALLOC_ERROR;
	  }
	node->do_option=do_option_func;
	node->next=parse_config_list;
	parse_config_list=node;
	return 0;
}

parse_config* get_parse_config_list(){
	return parse_config_list;
}

int make_level_to_int(char*  str)
{
	if(strncmp(str,"DEBUG",SHORTSTR)==0)
	  return DEBUG;
	else if(strncmp(str,"INFO",SHORTSTR)==0)
	  return INFO;
	else if(strncmp(str,"WARN",SHORTSTR)==0) 
	  return WARN;
	else if(strncmp(str,"ERROR",SHORTSTR)==0)
	  return ERROR;
    else if(strncmp(str,"FATAL",SHORTSTR)==0)
	  return FATAL;
	else return -1;	
}

int make_format_to_int(char* str)
{
	if(strncmp(str,"DATA",SHORTSTR)==0)
	  return LOGDATA;
	else if(strncmp(str,"TIME",SHORTSTR)==0)
	  return LOGTIME;
	else if(strncmp(str,"FILE",SHORTSTR)==0)
	  return LOGFILE;
	else if(strncmp(str,"LEVEL",SHORTSTR)==0)
	  return LOGLEVEL;
	else if(strncmp(str,"MESSAGE",SHORTSTR)==0)
	  return LOGMESSAGE;
	else return -1;
}

int parse_line_formatted(int params_num,const char *params[])
{
	parse_config *pc;
	int retval=0;
	if(parse_config_list==NULL)
	  return -1;
	pc=parse_config_list;
	if(params_num<1) return -1;
	while(pc!=NULL)
	{
		if(!strncmp(pc->option_name,params[0],SHORTSTR))
		{
			if(retval=pc->do_option(params_num-1,&params[1])<0)
			  return -1;
			else return 0;
		}
	  pc=pc->next;
	}
	return -1;
}




int parse_line(char *line)
{
	char *ptr;
	char *params[SHORTSTR];
	int params_num;
	params_num=0;
	int retval=-1;
	if(line==NULL) return -1;
	ptr=strtok(line,"=");
	while(ptr)
	{
	  params[params_num]=malloc(strlen(ptr));
      strncpy(params[params_num],ptr,SHORTSTR);
	  params_num++;
	  ptr=strtok(NULL,",");
	}
   retval=parse_line_formatted(params_num,&params[0]);
   return retval;

}
	
int read_config(const char *config_file_name)
{
  //读配置文件
  FILE* file=fopen(config_file_name,"r");
  if(file==0)
  {
	  printf("Read config file error");
	  exit(1);  //TOFIX  异常发生时，应该加入内存清理程序，此处有严重的内存泄漏!!
  }

  char line[LONGSTR]={0};
 do{
      fgets(line,sizeof(line)-1,file);
	  //除去末尾的空格和换行符
	  while(line[strlen(line)-1]=='\n' || line[strlen(line)-1]==' ')
		line[strlen(line)-1]=0;
	  if(parse_line(line)<0)
		return GET_CONFIG_INF_ERROR;
 }while(!feof(file));
 return 0;
}
